<?php

/**
 * 订单管理
 * @author laizhiyong <1029460965@qq.com>
 */

namespace app\manage\controller;
use app\common\model\User;
use app\common\model\Order as OrderModel;
use think\Db;

class Order extends Common
{
	/**
	 * 订单列表
	 * @return [type] [description]
	 */
	public function lists(){
        $pageSize = isset($pageSize)?$pageSize:15;
        $uid = session('user.id');
        $uid = 20;
        $type  = input('?get.type') ? input('get.type') : 0;
        $state = input('?get.state') ? input('get.state') : -2;
        $where = ['create_by' => $uid];
        $pageParam = [];
        if($type){
            $where['type'] = $type;
            $pageParam['query']['type']=$type;
        }

        if($state != -2){
            $where['state'] = $state;
            $pageParam['query']['state']=$state;
        }
        $this->assign('type',$type);
        $this->assign('state',$state);


        $rows = Db::table('affair_table')
            ->alias('a')
            ->field('a.id,a.create_t,a.state,a.type,a.order_no,a.order_name,u1.name create_name,u2.name charge_name')
            ->join('user u1','u1.uid=a.create_by','left')
            ->join('user u2','u2.uid=a.charge','left')
            ->where($where)
            ->order('id desc')
            ->paginate($pageSize,false,$pageParam);

		return view('',['data'=>$rows,'title'=>'订单列表']);
	}

	/**
	 * 业务审核列表
	 * @return [type] [description]
	 */
	public function examine(){
        $pageSize = isset($pageSize)?$pageSize:15;
        $uid = session('user.id');
        $uid = 20;
        $type = input('get.type',0);
        $where = [
            'cur_chief' => $uid,
            'state' => -1
        ];
        $pageParam = [];
        if($type){
            $where['type'] = $type;
            $pageParam['query']['type']=$type;
        }
        $this->assign('type',$type);

        $rows = Db::table('affair_table')
            ->alias('a')
            ->field('a.id,a.create_t,a.type,a.order_no,a.order_name,u1.name create_name,u2.name charge_name')
            ->join('user u1','u1.uid=a.create_by','left')
            ->join('user u2','u2.uid=a.charge','left')
            ->where($where)
            ->order('id desc')
            ->paginate($pageSize,false,$pageParam);

        return view('',['data'=>$rows,'title'=>'审核列表']);

	}

    /**
     * 发货列表
     * @return [type] [description]
     */
    public function sendlist(){
        $pageSize = isset($pageSize)?$pageSize:15;
        $uid = session('user.id');
        $uid = 20;

        $logistics = input('?get.logistics') ? input('get.logistics') : -1;
        $where = ['type'=>2,'state'=>1];
        $pageParam = [];

        if($logistics != -1){
            $where['logistics'] = $logistics;
            $pageParam['query']['logistics']=$logistics;
        }
        $this->assign('logistics',$logistics);

        $res = Db::table('affair_table')
            ->alias('a')
            ->where($where)
            ->order('id desc')
            ->paginate($pageSize,false,$pageParam);

        //已发货的订单超出1周后默认为已收货
        if (!empty($res)){
            foreach ($res as $v){
                //已发货
                if ($v['logistics'] == 1){
                    $arrival = $v['arrival_t'];
                    $arrival_t = json_decode($arrival,true);
                    $arrival = $arrival_t['etime'];
                    $end_t = $arrival+7*24*3600;
                    if ($end_t < time()){
                        //修改为已收货
                        Db::table('affair_table')->where(['id'=>$v['id']])->update(['logistics'=>2]);
                    }
                }
            }
        }

        //发货列表
        $rows = Db::table('affair_table')
            ->alias('a')
            ->field('a.id,a.arrival_t,a.create_t,a.state,a.type,a.order_no,a.order_name,a.logistics,u1.name create_name,u2.name charge_name')
            ->join('user u1','u1.uid=a.create_by','left')
            ->join('user u2','u2.uid=a.charge','left')
            ->where($where)
            ->order('id desc')
            ->paginate($pageSize,false,$pageParam);

        return view('',['data'=>$rows,'title'=>'发货列表']);
    }

    /**
     * 添加修改物流信息
     * @return [type] [description]
     */
    public function logistics(){
        if (request()->isAjax()){
            $id = input('post.id',false);
            $lid = input('post.lid',false); //物流编号
            $time = input('post.time',false); //预计时间
            if (!$lid || !$time || !$id) show(0,'物流信息不够完善，请重新提交');
            $transport =json_encode(['lid'=>$lid,'stime'=>time()]);
            $arrival_t =json_encode(['etime'=>strtotime($time)]);
            $res = Db::table('affair_table')
                ->where(['type'=>2,'state'=>1,'id'=>$id])
                ->update(['transport'=>$transport,'arrival_t'=>$arrival_t,'logistics'=>1]);
            if ($res) show(1,'提交成功');
        }
    }

    /**
     * 获取物流信息
     * @return [type] [description]
     */
    public function getlogistic(){
        if (request()->isPost()){
            $id = input('post.id',false);
            $res = Db::table('affair_table')
                ->where(['type'=>2,'state'=>1,'id'=>$id])
                ->find();
            //物流信息
            $transport = $res['transport'];
            $lid = '';
            $sendtime = '';
            if (!empty($transport)){
                $transport = json_decode($transport,true);
                $lid = $transport['lid'];
                $sendtime =  date('Y-m-d',$transport['stime']);

            }
            $etime = $res['arrival_t'];
            if (!empty($etime)){
                $etime = json_decode($etime,true);
                $etime = date('Y-m-d',$etime['etime']);
            }
            $logistics = $res['logistics'];
            $data = [
                'transport'=>$lid,
                'arrival_t'=>$etime,
                'logistics'=>$logistics,
                'stime'=>$sendtime,
            ];
            if ($res) show(1,'',$data);
        }
    }

    /**
     * 搜索获取抄送人
     * @return [type] [description]
     */
    public function getresponseinfo(){
        if (request()->isAjax()){
            $type =input('post.type') ? input('post.type') : 0;
            $name = input('post.name','');
            $where=['valid'=>1];
            if ($type) $where['name'] = $name;
            $user = model('user')
                ->field('uid')
                ->where($where)
                ->select();
            $user = endoceArr($user);
            $userModel = model('user');
            $res = [];
            if ($user){
                foreach ($user as $vo){
                    $data = $userModel
                        ->alias('u')
                        ->field('u.uid,u.name,group_concat(r2.name SEPARATOR "|") rname')
                        ->join([
                            ['relation r1','u.uid=r1.uid','left'],
                            ['role r2','r2.id=r1.rid','left'],
                        ])
                        ->where(['r2.is_del'=>0,'u.uid'=>$vo['uid']])
                        ->find();
                    if ($data){
                        $data = $data->toArray();
                    }
                    $res[]=$data;
                }
            }
            show(1,'',$res);
        }
    }

    /**
     * 确认收货
     * @return [type] [description]
     */
    public function receipt(){
        if (request()->isAjax()){
            $id = input('id');
            $res = Db::table('affair_table')
                ->where(['id'=>$id])
                ->update(['logistics'=>2]);
            if ($res) show(1,'提交成功');
            show(0,'提交失败');
        }

    }

    /**
     * 发货详情
     * @return [type] [description]
     */
    public function loginfo(){
        return view('',['title'=>'详情']);
    }

	/**
	 * 产品订单详情
	 * @return [type] [description]
	 */
	public function oinfos(){
		return view('',['title'=>'产品详情']);
	}

	/**
	 * 工程订单详情
	 * @return [type] [description]
	 */
	public function ginfos(){
		return view('',['title'=>'详情']);
	}

    /**
     * 产品订单审核详情
     * @return [type] [description]
     */
	public function exinfos(){
	    return view('',['title'=>'产品审核']);
    }

    /**
     * 工程订单审核详情
     * @return [type] [description]
     */
    public function exginfos(){
        return view('',['title'=>'工程审核']);
    }

    /**
     * 评审提交及评语
     */
    public function isexamine(){
       if (request()->isAjax()){
           $oid = input('id');
           $pas = input('pas');
           $comment = input('post.comment');
           $orderModel = new OrderModel();
           $res = $orderModel->updInfo(['id'=>$oid],['isexamine'=>$pas,'comment'=>$comment]);
           if($res){
               show(1,'修改成功');
           }else{
               show(0,'修改失败,请重试!');
           }
       }
    }


	//征询意见
	public function inquiry(){
		return view('', ['title'=>'信息征询']);
	}
	
	//抄送界面
	public function copy(){
		return view('', ['title'=>'订单抄送']);
	}
	
	//重修
	public function rework(){
		return view('', ['title'=>'提交订单']);
	}
	
	//返修
	public function modify(){
		return view('', ['title'=>'修改订单']);
	}
	
	//工程征询意见
	public function ginquiry(){
		return view('', ['title'=>'信息征询']);
	}
	//抄送界面
	public function gcopy(){
		return view('', ['title'=>'订单抄送']);
	}
}